 <?php
/**
 * 后台，用户管理控制器.
 *
 * @author liuxiaoqing <liuxiaoqing@zhishisoft.com>
 *
 * @version TS3.0
 */
// 加载后台控制器
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');
class AdminAction extends AdministratorAction
{
    public $pageTitle = array();

    /**
     * 初始化，初始化页面表头信息，用于双语.
     */
    public function _initialize()
    {
        $this->pageTitle['index'] = '队伍列表';
        $this->pageTitle['addTeam'] = '添加队伍';
        $this->pageTitle['teamCate'] = '队伍分类';
        $this->pageTitle['addTeamCate'] = '添加分类';
        $this->pageTitle['editTeamCate'] = '编辑分类';
        $this->pageTitle['postList'] = '帖子列表';
        $this->pageTitle['postRecycle'] = '帖子回收站';
        $this->pageTitle['teamAdminAuditConfig'] = '申请队长配置';
        $this->pageTitle['teamAdminAudit'] = '队长审核';
        $this->pageTitle['teamAuditConfig'] = '申请队伍配置';
        $this->pageTitle['teamAudit'] = '队伍审核';
        $this->pageTitle['indexPost'] = '首页帖子';
        parent::_initialize();
    }

    /**
     * 队伍列表.
     */
    public function index()
    {
        // 初始化队伍列表管理菜单
        $this->_initTeamListAdminMenu();
        // 设置列表主键
        $this->_listpk = 'team_id';
        $this->pageButton[] = array('title' => '搜索队伍', 'onclick' => "admin.fold('search_form')");
        $this->pageButton[] = array('title' => '解散队伍', 'onclick' => 'admin.delTeam()');
        $this->searchKey = array('team_id', 'team_name', 'team_cate', 'uid', 'admin_uid', 'recommend');
        $this->opt['recommend'] = array('0' => L('PUBLIC_SYSTEMD_NOACCEPT'), '1' => '是', '2' => '否');
        $teamcate = D('team_category')->findAll();
        $cids = array();
        foreach ($teamcate as $c) {
            $cids[$c['id']] = $c['name'];
        }
        $this->opt['team_cate'] = $cids;
        $this->pageKeyList = array('team_id', 'team_name', 'team_cate', 'logo', 'uid', 'ctime', 'admin_uid', 'follower_count/thread_count', 'DOACTION');
        // 数据的格式化与listKey保持一致
        $listData = D('Team', 'team')->getTeamList(20);
        $this->displayList($listData);
    }

    /**
     * 添加队伍.
     */
    public function addTeam()
    {
        // 初始化队伍列表管理菜单
        $this->_initTeamListAdminMenu();
        // 列表key值 DOACTION表示操作
        $this->pageKeyList = array('team_name', 'cid', 'logo', 'intro', 'who_can_post', 'admin_uid', 'recommend');
        $this->opt['who_can_post'] = array('0' => '所有人', '1' => '队内成员', 2 => '队伍管理员', 3 => '队伍队内');
        $this->opt['recommend'] = array('1' => L('PUBLIC_SYSTEMD_TRUE'), '0' => L('PUBLIC_SYSTEMD_FALSE'));
        $list = D('TeamCategory')->getAllTeamCate();
        $this->opt['cid'] = $list;
        // 表单URL设置
        $this->savePostUrl = U('team/Admin/doAddTeam');
        $this->notEmpty = array('team_name', 'cid', 'logo', 'intro');
        $this->onsubmit = 'admin.checkAddTeam(this)';
        $this->displayConfig();
    }

    /**
     * 执行添加队伍.
     */
    public function doAddTeam()
    {
        //dump($_POST);exit;
        $data['team_name'] = t($_POST['team_name']);
        $data['is_del'] = 0;
        if (D('team')->where($data)->find()) {
            $this->error('此队伍已存在');
        }
        $data['cid'] = intval($_POST['cid']);
        if (empty($data['cid'])) {
            $this->error('队伍分类不能为空');
        }
        $data['uid'] = $this->mid;
        $data['ctime'] = time();
        $data['logo'] = t($_POST['logo']);
        $data['intro'] = $_POST['intro'];
        $data['who_can_post'] = intval($_POST['who_can_post']);
        if ($_POST['admin_uid']) {
            $data['admin_uid'] = intval($_POST['admin_uid']);
            $data['follower_count'] = 1;
        } else {
            $data['admin_uid'] = 0;
        }

        $data['recommend'] = intval($_POST['recommend']);
        $data['status'] = 1;

        // # 修复，后台上传队伍logo前台没有logo
        if ($data['logo']) {
            $data['avatar_big'] = getImageUrlByAttachId($data['logo'], 200, 200);
            $data['avatar_middle'] = getImageUrlByAttachId($data['logo'], 100, 100);
        }

        $res = D('Team', 'team')->add($data);

        if ($res) {
            if ($_POST['admin_uid']) {      //超级队内加入队伍
                $follow['follower_uid'] = $data['admin_uid'];
                $follow['team_id'] = $res;
                $follow['level'] = 3;
                D('team_follow')->add($follow);
            }
            if ($data['admin_uid'] != $this->mid) {    //创建者加入队伍
                $follows['follower_uid'] = $this->mid;
                $follows['team_id'] = $res;
                $follows['level'] = 1;
                D('team_follow')->add($follows);
                D('team')->where('team_id='.$res)->setInc('follower_count');
            }
            $this->assign('jumpUrl', U('team/Admin/index'));
            $this->success(L('PUBLIC_ADD_SUCCESS'));
        } else {
            $this->error(D('Team', 'team')->getLastError());
        }
    }

    /**
     * 编辑队伍.
     */
    public function editTeam()
    {
        $this->assign('pageTitle', '编辑队伍');
        // 初始化队伍列表管理菜单
// 		$this->_initTeamListAdminMenu();
        $this->pageTab[] = array('title' => '队伍列表', 'tabHash' => 'index', 'url' => U('team/Admin/index'));
        //$this->pageTab[] = array('title'=>'添加队伍','tabHash'=>'addTeam','url'=>U('team/Admin/addTeam'));
        $this->pageTab[] = array('title' => '队伍分类', 'tabHash' => 'teamCate', 'url' => U('team/Admin/teamCate'));
        $this->pageTab[] = array('title' => '编辑队伍', 'tabHash' => 'editTeam', 'url' => U('team/Admin/editTeam', array('team_id' => $_GET['team_id'])));
        $this->pageTab[] = array('title' => '帖子列表', 'tabHash' => 'postList', 'url' => U('team/Admin/postList'));
        $this->pageTab[] = array('title' => '帖子回收站', 'tabHash' => 'postRecycle', 'url' => U('team/Admin/postRecycle'));
        // 列表key值 DOACTION表示操作
        $this->pageKeyList = array('team_id', 'team_name', 'cid', 'logo', 'intro', 'notify', 'who_can_post', 'admin_uid', 'recommend');
        $list = D('TeamCategory')->getAllTeamCate();
        $this->opt['cid'] = $list;
        $this->opt['who_can_post'] = array('0' => '所有人', '1' => '队内成员', 2 => '队伍管理员', 3 => '队伍队内');
        $this->opt['recommend'] = array('1' => L('PUBLIC_SYSTEMD_TRUE'), '0' => L('PUBLIC_SYSTEMD_FALSE'));
        $team_id = intval($_GET['team_id']);
        $data = D('team', 'team')->getTeamById($team_id);
        if (!$data['admin_uid']) {
            $data['admin_uid'] = '';
        }
        // 表单URL设置
        $this->savePostUrl = U('team/Admin/doEditTeam');
        $this->notEmpty = array('team_name', 'cid', 'logo', 'intro');
        $this->onsubmit = 'admin.checkAddTeam(this)';

        $this->displayConfig($data);
    }

    /**
     * 执行编辑队伍.
     */
    public function doEditTeam()
    {
        $team_id = intval($_POST['team_id']);
        $data['team_name'] = t($_POST['team_name']);
        $map['team_id'] = array('neq', $team_id);
        $map['team_name'] = $data['team_name'];
        $map['is_del'] = 0;
        if (D('team')->where($map)->find()) {
            $this->error('此队伍已存在');
        }
        //$data['uid'] = $this->mid;
        $data['cid'] = intval($_POST['cid']);
        if (empty($data['cid'])) {
            $this->error('队伍分类不能为空');
        }
        $data['logo'] = t($_POST['logo']);
        $data['intro'] = $_POST['intro'];
        $data['notify'] = t($_POST['notify']);
        $data['who_can_post'] = t($_POST['who_can_post']);
        $data['admin_uid'] = intval($_POST['admin_uid']);
        $data['recommend'] = intval($_POST['recommend']);

        // # 修复，后台上传队伍logo前台没有logo
        if ($data['logo']) {
            $data['avatar_big'] = getImageUrlByAttachId($data['logo'], 200, 200);
            $data['avatar_middle'] = getImageUrlByAttachId($data['logo'], 100, 100);
        }

        $res = D('team')->where('team_id='.$team_id)->save($data);
        if ($res !== false) {
            //现有超级队内
            $follow['level'] = 3;
            $follow['team_id'] = $team_id;
            $admin_uid = D('team_follow')->where($follow)->getField('follower_uid');
            if ($admin_uid && $admin_uid != $data['admin_uid']) {  //如果存在队内并且设置了新队内，则原队内降为普通成员
                $a['follower_uid'] = $admin_uid;
                $a['team_id'] = $team_id;
                D('team_follow')->where($a)->setField('level', 1);
            }
            if ($data['admin_uid']) {
                $follows['follower_uid'] = $data['admin_uid'];
                $follows['team_id'] = $team_id;
                if (D('team_follow')->where($follows)->find()) {  //该队内已经为成员
                    D('team_follow')->where($follows)->where($follows)->setField('level', 3);
                } else {
                    $follows['level'] = 3;
                    D('team_follow')->add($follows);
                }
            }
            $this->assign('jumpUrl', U('team/Admin/index'));
            $this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
        } else {
            $this->error(D('team')->getLastError());
        }
    }

    /**
     * 队伍分类列表.
     */
    public function teamCate()
    {
        // 初始化队伍列表管理菜单
        $this->_initTeamListAdminMenu();
        $this->pageKeyList = array('id', 'name', 'DOACTION');
        $this->pageButton[] = array('title' => '添加分类', 'onclick' => "javascript:location.href='".U('team/Admin/addTeamCate', array('tabHash' => 'teamCate'))."';");
        $this->pageButton[] = array('title' => '删除分类', 'onclick' => 'admin.delTeamCate()');
        $list = D('team_category')->findPage();
        foreach ($list['data'] as &$v) {
            $v['DOACTION'] = "<a href='".U('team/Admin/editTeamCate', array('id' => $v['id'], 'tabHash' => 'teamCate'))."'>编辑</a>&nbsp;-&nbsp;<a href='javascript:;' onclick='admin.delTeamCate(".$v['id'].")'>删除</a>";
        }
        $this->displayList($list);
    }

    /**
     * 添加队伍分类页面.
     */
    public function addTeamCate()
    {
        // 初始化队伍列表管理菜单
        $this->_initTeamListAdminMenu();
        $this->pageKeyList = array('name');
        $this->savePostUrl = U('team/Admin/doAddTeamCate');
        $this->displayConfig();
    }

    /**
     * 添加队伍分类数据.
     */
    public function doAddTeamCate()
    {
        $name = t($_POST['name']);
        if ($name) {
            $data['name'] = $name;
            $exist = D('TeamCategory')->where($data)->find();
            if ($exist) {
                $this->error('已存在相同分类！');
            }
            $res = D('TeamCategory')->add($data);
            if ($res) {
                $this->assign('jumpUrl', U('team/Admin/teamCate', array('tabHash' => 'teamCate')));
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->error('分类不能为空');
        }
    }

    /**
     * 编辑队伍分类页面.
     */
    public function editTeamCate()
    {
        // 初始化队伍列表管理菜单
        $this->_initTeamListAdminMenu();
        $id = intval($_GET['id']);
        $data = D('TeamCategory')->where('id='.$id)->find();
        $this->pageKeyList = array('id', 'name');
        $this->savePostUrl = U('team/Admin/doEditTeamCate');
        $this->displayConfig($data);
    }

    /**
     * 编辑队伍分类数据.
     */
    public function doEditTeamCate()
    {
        $name = t($_POST['name']);
        $id = intval($_POST['id']);
        if ($name) {
            $data['name'] = $name;
            $map['name'] = $name;
            $map['id'] = array('neq', $id);
            $exist = D('TeamCategory')->where($map)->find();
            if ($exist) {
                $this->error('已存在相同分类！');
            }
            $res = D('TeamCategory')->where('id='.$id)->save($data);
            if ($res) {
                $this->assign('jumpUrl', U('team/Admin/teamCate', array('tabHash' => 'teamCate')));
                $this->success('编辑成功');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $this->error('分类不能为空');
        }
    }

    /**
     * 删除队伍分类.
     */
    public function delTeamCate()
    {
        $ids = $_POST['cate_id'];
        if ($ids) {
            !is_array($ids) && array($ids);
            $map['id'] = array('in', $ids);
            $wmap['cid'] = array('in', $ids);
            $wmap['is_del'] = 0;
            $exist = D('Team')->where($wmap)->find();
            if ($exist) {
                $return['status'] = 0;
                $return['data'] = '删除失败，该分类下还有队伍！';
                exit(json_encode($return));
            }
            $res = D('TeamCategory')->where($map)->delete();
            if ($res) {
                $return['status'] = 1;
                $return['data'] = '删除成功';
            } else {
                $return['status'] = 0;
                $return['data'] = '删除失败';
            }
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 设置队伍推荐状态
     *
     * @return array 操作成功状态和提示信息
     */
    public function setRecommend()
    {
        if (empty($_POST['team_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        if (intval($_POST['type']) == 1) {
            $value = 0;
        } else {
            $value = 1;
        }
        $team_id = intval($_POST['team_id']);
        $result = D('team')->where('team_id='.$team_id)->setField('recommend', $value);
        $uid = D('team')->where('team_id='.$team_id)->getField('uid');
        //添加积分
        if ($value == 1) {
            model('Credit')->setUserCredit($uid, 'recommended_team');
        }

        if (!$result) {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        } else {
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 解散队伍.
     *
     * @return array 操作成功状态和提示信息
     */
    public function delTeam()
    {
        if (empty($_POST['team_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        !is_array($_POST['team_id']) && $_POST['team_id'] = array($_POST['team_id']);
        $data['team_id'] = array('in', $_POST['team_id']);
        $result = D('team')->where($data)->setField('is_del', 1);
        if ($result) {
            D('team_post')->where('team_id='.$team_id)->delete();
            D('team_reply')->where('team_id='.$team_id)->delete();
            D('team_follow')->where('team_id='.$team_id)->delete();
            D('team_log')->where('team_id='.$team_id)->delete();
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        } else {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 后台帖子列表.
     */
    public function postList()
    {
        $_REQUEST['tabHash'] = 'postList';
        $this->_initTeamListAdminMenu();
        // 设置列表主键
        $this->_listpk = 'post_id';
        $this->pageButton[] = array('title' => '搜索帖子', 'onclick' => "admin.fold('search_form')");
        // $this->pageButton[] = array('title'=>'调整回复楼层','onclick'=>"admin.doStorey()");
        $this->pageButton[] = array('title' => '删除帖子', 'onclick' => 'admin.delPost()');
        $this->searchKey = array('post_id', 'title', 'post_uid', 'recommend', 'digest', 'top', 'team_id');
        $this->opt['recommend'] = array('0' => L('PUBLIC_SYSTEMD_NOACCEPT'), '1' => '是', '2' => '否');
        $this->opt['digest'] = array('0' => L('PUBLIC_SYSTEMD_NOACCEPT'), '1' => '是', '2' => '否');
        $this->opt['top'] = array('0' => L('PUBLIC_SYSTEMD_NOACCEPT'), '1' => '队内置顶', '2' => '全局置顶');
        $teamList = D('team')->getHashList($k = 'team_id', $v = 'team_name');
        $teamList[0] = L('PUBLIC_SYSTEMD_NOACCEPT');
        $this->opt['team_id'] = $teamList;
        $this->pageKeyList = array('post_id', 'title', 'post_uid', 'post_time', 'last_reply_time', 'read_count/reply_count', 'team_id', 'DOACTION');
        // 数据的格式化与listKey保持一致
        $listData = D('Team', 'team')->getPostList(20, array('is_del' => 0));
        $this->displayList($listData);
    }

    /**
     * 首页帖子列表.
     */
    public function indexPost()
    {
        $_REQUEST['tabHash'] = 'indexPost';
        $this->_initTeamListAdminMenu();
        // 设置列表主键
        $this->_listpk = 'post_id';
        $this->pageButton[] = array('title' => '移除帖子', 'onclick' => 'admin.removePost()');
        $this->opt['recommend'] = array('0' => L('PUBLIC_SYSTEMD_NOACCEPT'), '1' => '是', '2' => '否');
        $this->opt['digest'] = array('0' => L('PUBLIC_SYSTEMD_NOACCEPT'), '1' => '是', '2' => '否');
        $this->opt['top'] = array('0' => L('PUBLIC_SYSTEMD_NOACCEPT'), '1' => '队内置顶', '2' => '全局置顶');
        $teamList = D('team')->getHashList($k = 'team_id', $v = 'team_name');
        $teamList[0] = L('PUBLIC_SYSTEMD_NOACCEPT');
        $this->opt['team_id'] = $teamList;
        $this->pageKeyList = array('post_id', 'title', 'post_uid', 'index_img', 'is_index_time', 'team_id', 'DOACTION');
        // 查询数据
        $map['team_id'] = array(
                'in',
                getSubByKey(M('team')->where('is_del=0')->findAll(), 'team_id'),
        );
        $map['is_del'] = 0;
        $map['is_index'] = 1;
        $list = M('team_post')->where($map)->order('last_reply_time desc,post_time desc')->findPage(5);
        $scount = M('team_post')->where($map)->count();
        if ($scount < 5) {
            $this->pageButton[] = array('title' => '添加帖子', 'onclick' => "location.href='".U('team/Admin/addIndexPost')."'");
        }
        // 数据组装
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['title'] = '<a target="_blank" href="'.U('team/Index/postDetail', array(
                    'post_id' => $v['post_id'],
            )).'">'.$v['title'].'</a>';
            $author = model('User')->getUserInfoByUids($v['post_uid']);
            $list['data'][$k]['post_uid'] = $author[$v['post_uid']]['space_link'];
            $list['data'][$k]['index_img'] = '<img src="'.getImageUrlByAttachId($v['index_img']).'"  style="height:50px; width:50px;" />';
            $list['data'][$k]['is_index_time'] = friendlyDate($v['is_index_time']);
            $list['data'][$k]['team_id'] = M('team')->where('team_id='.$v['team_id'])->getField('team_name');
            if ($v['is_del'] == 0) {
                $list['data'][$k]['DOACTION'] = '<a href='.U('team/Admin/editIndexPost', 'post_id='.$v['post_id']).'>修改</a>&nbsp;-&nbsp;';

                $list['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.removePost('.$v['post_id'].')">移除帖子</a>';
            } else {
                $list['data'][$k]['DOACTION'] = '<a href="javascript:void(0)" onclick="admin.recoverPost('.$v['post_id'].')">还原</a>&nbsp;-&nbsp;<a href="javascript:void(0)" onclick="admin.deletePost('.$v['post_id'].')">彻底删除</a>';
            }
            $listData = $list;
        }
        $this->displayList($listData);
    }

    /**
     * 添加首页帖子.
     */
    public function addIndexPost()
    {
        $_REQUEST['tabHash'] = 'addIndexPost';
        // 初始化队伍列表管理菜单
        $this->_initTeamListAdminMenu();
        $this->pageTab[] = array('title' => '添加首页帖子', 'tabHash' => 'addIndexPost', 'url' => U('team/Admin/addIndexPost'));
        // 列表key值 DOACTION表示操作
        $this->pageKeyList = array('post_id', 'index_img');
        // 表单URL设置
        $this->savePostUrl = U('team/Admin/doIndexPost');
        $this->notEmpty = array('post_id', 'index_img');
        $this->onsubmit = 'admin.checkNewImg(this)';
        $this->displayConfig();
    }

    /**
     * 编辑首页帖子.
     */
    public function editIndexPost()
    {
        $_REQUEST['tabHash'] = 'editIndexPost';
        if ($_GET['post_id'] != '') {
            $imgs = M('team_post')->where('post_id='.$_GET['post_id'])->find();
        }
        // 初始化队伍列表管理菜单
        $this->_initTeamListAdminMenu();
        $this->pageTab[] = array('title' => '编辑首页帖子', 'tabHash' => 'editIndexPost', 'url' => U('team/Admin/editIndexPost'));
        // 列表key值 DOACTION表示操作
        $this->pageKeyList = array('post_id', 'index_img');
        // 表单URL设置
        $this->savePostUrl = U('team/Admin/doIndexPost');
        $this->notEmpty = array('index_img');
        $this->onsubmit = 'admin.checkNewImg(this)';
        $this->displayConfig($imgs);
    }

    /**
     * 添加/修改首页帖子.
     */
    public function doIndexPost()
    {
        $img = M('team_post')->where('post_id='.(int) $_POST['post_id'])->find();
        if ($_POST['index_img'] == '') {
            $this->error('请上传图片!');
        } else {
            $data['index_img'] = (int) $_POST['index_img'];
            $data['is_index'] = 1;
            $data['is_index_time'] = time();
            if ($img && $img['is_del'] != 1) {
                $res = M('team_post')->where('post_id='.$img['post_id'])->save($data);
                //dump(M()->getLastSql());
                //dump($res);exit;
            } else {
                $this->error('该帖子已经删除!');
            }
            if ($res) {
                $this->assign('jumpUrl', U('team/Admin/indexPost'));
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /**
     * 帖子回收站.
     */
    public function postRecycle()
    {
        $_REQUEST['tabHash'] = 'postRecycle';
        $this->_initTeamListAdminMenu();
        // 设置列表主键
        $this->_listpk = 'post_id';
        $this->pageButton[] = array('title' => '搜索帖子', 'onclick' => "admin.fold('search_form')");
        $this->pageButton[] = array('title' => '还原', 'onclick' => 'admin.recoverPost()');
        $this->pageButton[] = array('title' => '彻底删除', 'onclick' => 'admin.deletePost()');
        $this->searchKey = array('post_id', 'title', 'post_uid', 'team_id');
        $teamList = D('team')->getHashList($k = 'team_id', $v = 'team_name');
        $teamList[0] = L('PUBLIC_SYSTEMD_NOACCEPT');
        $this->opt['team_id'] = $teamList;
        $this->pageKeyList = array('post_id', 'title', 'post_uid', 'post_time', 'last_reply_time', 'read_count/reply_count', 'team_id', 'DOACTION');
        // 数据的格式化与listKey保持一致
        $listData = D('Team', 'team')->getPostList(20, array('is_del' => 1));
        $this->displayList($listData);
    }

    /**
     * 设置帖子状态
     *
     * @return array 操作成功状态和提示信息
     */
    public function setPost()
    {
        if (empty($_POST['post_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        $post_detail = D('team_post')->where('post_id='.$post_id)->find();
        switch ($_POST['type']) {
            case '1':         //推荐
                $field = 'recommend';
                if (intval($_POST['curValue']) == 1) {
                    $value = 0;
                } else {
                    $value = 1;
                }
                break;
            case '2':         //精华
                $field = 'digest';
                if (intval($_POST['curValue']) == 1) {
                    $value = 0;
                } else {
                    $value = 1;
                }
                break;
            case '3':         //置顶
                $field = 'top';
                if (intval($_POST['curValue']) == intval($_POST['topValue'])) {
                    $value = 0;
                } else {
                    $value = intval($_POST['topValue']);
                }
                break;
        }
        $post_id = intval($_POST['post_id']);
        $result = D('team_post')->where('post_id='.$post_id)->setField($field, $value);
        if ($field == 'top' && $value == 2) {
            D('team_post')->where('post_id='.$post_id)->setField('top_time', time());
        }
        if (!$result) {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        } else {
            $post_detail = D('team_post')->where('post_id='.$post_id)->find();
            $config['post_name'] = $post_detail['title'];
            $config['post_url'] = '<a href="'.U('team/Index/postDetail', array('post_id' => $post_id)).'" target="_blank">'.$post_detail['title'].'</a>';
            switch ($_POST['type']) {
                case '1':         //推荐
                    //添加积分
                    if ($value == 1) {
                        model('Credit')->setUserCredit($post_detail['post_uid'], 'recommend_topic');
                    }
                    break;
                case '2':         //精华
                    if ($value == 1) {
                        $config['typename'] = '精华';
                        model('Notify')->sendNotify($post_detail['post_uid'], 'team_post_set', $config);
                        //添加积分
                        model('Credit')->setUserCredit($post_detail['post_uid'], 'dist_topic');
                    }

                    break;
                case '3':         //置顶
                    if ($value == 1) {
                        $config['typename'] = '队内置顶';
                        model('Notify')->sendNotify($post_detail['post_uid'], 'team_post_set', $config);
                        //添加积分
                        model('Credit')->setUserCredit($post_detail['post_uid'], 'top_topic_team');
                    } elseif ($value == 2) {
                        $config['typename'] = '全局置顶';
                        model('Notify')->sendNotify($post_detail['post_uid'], 'team_post_set', $config);
                        //添加积分
                        model('Credit')->setUserCredit($post_detail['post_uid'], 'top_topic_all');
                    }
                    break;
            }
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 后台编辑帖子.
     */
    public function editPost()
    {
        $this->assign('pageTitle', '编辑帖子');
        // 初始化队伍列表管理菜单
        $this->pageTab[] = array('title' => '队伍列表', 'tabHash' => 'index', 'url' => U('team/Admin/index'));
        //$this->pageTab[] = array('title'=>'添加队伍','tabHash'=>'addTeam','url'=>U('team/Admin/addTeam'));
        $this->pageTab[] = array('title' => '队伍分类', 'tabHash' => 'teamCate', 'url' => U('team/Admin/teamCate'));
        $this->pageTab[] = array('title' => '帖子列表', 'tabHash' => 'postList', 'url' => U('team/Admin/postList'));
        $this->pageTab[] = array('title' => '编辑帖子', 'tabHash' => 'editPost', 'url' => U('team/Admin/editPost', array('post_id' => $_GET['post_id'])));
        $this->pageTab[] = array('title' => '帖子回收站', 'tabHash' => 'postRecycle', 'url' => U('team/Admin/postRecycle'));
        // 列表key值 DOACTION表示操作
        $this->pageKeyList = array('post_id', 'title', 'content', 'recommend', 'digest', 'top');
        $this->opt['recommend'] = array('1' => L('PUBLIC_SYSTEMD_TRUE'), '0' => L('PUBLIC_SYSTEMD_FALSE'));
        $this->opt['digest'] = array('1' => L('PUBLIC_SYSTEMD_TRUE'), '0' => L('PUBLIC_SYSTEMD_FALSE'));
        $this->opt['top'] = array('0' => L('PUBLIC_SYSTEMD_FALSE'), '1' => '队内置顶', '2' => '全局置顶');
        $post_id = intval($_GET['post_id']);
        $data = D('team_post')->where('post_id='.$post_id)->find();
        // 表单URL设置
        $this->savePostUrl = U('team/Admin/doEditPost');
        $this->notEmpty = array('title', 'content');
        $this->onsubmit = 'admin.checkEditPost(this)';
        $this->displayConfig($data);
    }

    /**
     * 执行编辑帖子.
     */
    public function doEditPost()
    {
        $checkContent = str_replace('&nbsp;', '', $_POST['content']);
        $checkContent = str_replace('<br />', '', $checkContent);
        $checkContent = str_replace('<p>', '', $checkContent);
        $checkContent = str_replace('</p>', '', $checkContent);
        $checkContents = preg_replace('/<img(.*?)src=/i', 'img', $checkContent);
        if (strlen(t($_POST['title'])) == 0) {
            $this->error('帖子标题不能为空');
        }
        if (strlen(t($checkContents)) == 0) {
            $this->error('帖子内容不能为空');
        }
        $post_id = intval($_POST['post_id']);
        $data['title'] = t($_POST['title']);
        $data['content'] = h($_POST['content']);
        $data['recommend'] = intval($_POST['recommend']);
        $data['digest'] = intval($_POST['digest']);
        $data['top'] = intval($_POST['top']);
        $res = D('team_post')->where('post_id='.$post_id)->save($data);
        if ($res !== false) {
            //同步到分享
            $feed_id = D('team_post')->where('post_id='.$post_id)->getField('feed_id');
            $feedInfo = D('feed_data')->where('feed_id='.$feed_id)->find();
            $datas = unserialize($feedInfo['feed_data']);
            $datas['content'] = '【'.$data['title'].'】'.getShort(t($checkContent), 100).'&nbsp;';
            $datas['body'] = $datas['content'];
            $data1['feed_data'] = serialize($datas);
            $data1['feed_content'] = $datas['content'];
            D('feed_data')->where('feed_id='.$feed_id)->save($data1);
            model('Cache')->rm('fd_'.$feed_id);
            $this->assign('jumpUrl', U('team/Admin/postList', array('tabHash' => 'postList')));
            $this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
        } else {
            $this->error(D('team_post')->getLastError());
        }
    }

    /**
     * 后台删除帖子至回收站.
     */
    public function delPost()
    {
        if (empty($_POST['post_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        !is_array($_POST['post_id']) && $_POST['post_id'] = array($_POST['post_id']);
        $data['post_id'] = array('in', $_POST['post_id']);
        $res = D('team_post')->where($data)->setField('is_del', 1);
        if ($res) {
            $postList = D('team_post')->where($data)->findAll();
            foreach ($postList as $v) {
                D('team')->where('team_id='.$v['team_id'])->setDec('thread_count');
            }
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        } else {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        }
        echo json_encode($return);
        exit();
    }

    //index中移除该帖子
    public function removePost()
    {
        if (empty($_POST['post_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        !is_array($_POST['post_id']) && $_POST['post_id'] = array($_POST['post_id']);
        $data['post_id'] = array('in', $_POST['post_id']);
        $res = D('team_post')->where($data)->setField('is_index', 0);
        if ($res) {
            $postList = D('team_post')->where($data)->findAll();
            foreach ($postList as $v) {
                D('team')->where('team_id='.$v['team_id'])->setDec('thread_count');
            }
            $return['status'] = 1;
            $return['data'] = '移除成功';
        } else {
            $return['status'] = 0;
            $return['data'] = '移除失败';
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 调整评论楼层
     */
    public function doStorey()
    {
        if (empty($_POST['post_id'])) {
            echo 0;
            exit();
        }
        //echo 1;exit;
        !is_array($_POST['post_id']) && $_POST['post_id'] = array($_POST['post_id']);
        $data['post_id'] = array('in', $_POST['post_id']);
        $postList = D('team_post')->where($data)->findAll();
        foreach ($postList as $v) {
            $replyList = D('team_reply')->where('post_id='.$v['post_id'].' AND is_del=0')->order('reply_id ASC')->findAll();
            foreach ($replyList as $key => $val) {
                D('team_reply')->where('reply_id='.$val['reply_id'])->setField('storey', $key + 1);
            }
            D('team_post')->where('post_id='.$v['post_id'])->setField('reply_all_count', count($replyList)); //总回复统计数加1
        }
        echo 1;
        exit;
    }

    /**
     * 后台还原帖子.
     */
    public function recoverPost()
    {
        if (empty($_POST['post_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        !is_array($_POST['post_id']) && $_POST['post_id'] = array($_POST['post_id']);
        $data['post_id'] = array('in', $_POST['post_id']);
        $res = D('team_post')->where($data)->setField('is_del', 0);
        if ($res) {
            $postList = D('team_post')->where($data)->findAll();
            foreach ($postList as $v) {
                D('team')->where('team_id='.$v['team_id'])->setInc('thread_count');
            }
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        } else {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 后台删除帖子至回收站.
     */
    public function deletePost()
    {
        if (empty($_POST['post_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        !is_array($_POST['post_id']) && $_POST['post_id'] = array($_POST['post_id']);
        $data['post_id'] = array('in', $_POST['post_id']);
        $res = D('team_post')->where($data)->delete();
        if ($res) {
            D('team_reply')->where($data)->delete();
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        } else {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 队内审核配置.
     */
    public function teamAdminAuditConfig()
    {
        $this->_initTeamListAdminMenu();
        $this->pageKeyList = array('follower_open', 'follower', 'level_open', 'level', 'team_post_open', 'team_post');
        $this->savePostUrl = U('team/Admin/doTeamAdminAuditConfig');
        $this->opt['follower_open'] = array('0' => '否', '1' => '是');
        $this->opt['level_open'] = array('0' => '否', '1' => '是');
        $this->opt['team_post_open'] = array('0' => '否', '1' => '是');
        $teamAdminAuditConfig = model('Xdata')->get('team_Admin:teamAdminAuditConfig');
        // dump($teamAdminAuditConfig);exit;
        $this->displayConfig($teamAdminAuditConfig);
    }

    public function doTeamAdminAuditConfig()
    {
        $list = $_POST['systemdata_list'];
        $key = $_POST['systemdata_key'];
        $key = $list.':'.$key;
        $value['follower_open'] = intval($_POST['follower_open']);
        $value['follower'] = intval($_POST['follower']);
        $value['level_open'] = intval($_POST['level_open']);
        $value['level'] = intval($_POST['level']);
        $value['team_post_open'] = intval($_POST['team_post_open']);
        $value['team_post'] = intval($_POST['team_post']);
        $res = model('Xdata')->put($key, $value);
        if ($res) {
            $this->assign('jumpUrl', U('team/Admin/teamAdminAuditConfig', array('tabHash' => 'teamAdminAuditConfig')));
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 队内审核.
     */
    public function teamAdminAudit()
    {
        $_REQUEST['tabHash'] = 'teamAdminAudit';
        $this->_initTeamListAdminMenu();
        // 设置列表主键
        $this->_listpk = 'id';
        $this->pageButton[] = array('title' => '搜索', 'onclick' => "admin.fold('search_form')");
        // $this->pageButton[] = array('title'=>'通过','onclick'=>"admin.doAudit('', 1)");
        // $this->pageButton[] = array('title'=>'驳回','onclick'=>"admin.doAudit('', -1)");
        $this->searchKey = array('follower_uid', 'team_name');
        $this->pageKeyList = array('id', 'follower_uid', 'follower_uname', 'team_name', 'type', 'reason', 'DOACTION');
        !empty($_POST['follower_uid']) && $map['follower_uid'] = intval($_POST['follower_uid']);
        if (!empty($_POST['team_name'])) {
            $maps['team_name'] = array('like', '%'.t($_POST['team_name']).'%');
            $map['team_id'] = array('in', getSubByKey(D('team')->where($maps)->field('team_id')->findAll(), 'team_id'));
        }
        $map['status'] = 0;
        // 数据的格式化与listKey保持一致
        $listData = D('team_apply')->where($map)->findPage(20);
        foreach ($listData['data'] as $k => $v) {
            $userInfo = model('User')->getUserInfo($v['follower_uid']);
            $listData['data'][$k]['follower_uname'] = $userInfo['uname'];
            $listData['data'][$k]['team_name'] = D('team')->where('team_id='.$v['team_id'])->getField('team_name');
            switch ($v['type']) {
                case '2':
                    $listData['data'][$k]['type'] = '小主';
                    break;
                case '3':
                    $listData['data'][$k]['type'] = '队内';
                    break;
            }
            $listData['data'][$k]['DOACTION'] = '<a href="javascript:void(0)" onclick="admin.doAudit('.$v['team_id'].','.$v['follower_uid'].','.$v['type'].');">通过</a>&nbsp;|&nbsp;<a href="javascript:void(0)" onclick="admin.doAudit('.$v['team_id'].','.$v['follower_uid'].',-1);">驳回</a>';
        }
        $this->allSelected = false;
        $this->displayList($listData);
    }

    /**
     * 队内审核通过/驳回.
     */
    public function doAudit()
    {
        if (empty($_POST['id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        !is_array($_POST['id']) && $_POST['id'] = array($_POST['id']);
        $map['id'] = array('in', $_POST['id']);
        $data['status'] = intval($_POST['val']);
        $data['manager_uid'] = $this->mid;
        $res = D('team_apply')->where($map)->save($data);
        if ($res) {
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        } else {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 队伍审核配置.
     */
    public function teamAuditConfig()
    {
        $this->_initTeamListAdminMenu();
        $this->pageKeyList = array('apply_team_open', 'follower_open', 'follower', 'level_open', 'level', 'team_post_open', 'team_post', 'manager_open');
        $this->savePostUrl = U('team/Admin/doTeamAuditConfig');
        $this->opt['apply_team_open'] = array('0' => '否', '1' => '是');
        $this->opt['follower_open'] = array('0' => '否', '1' => '是');
        $this->opt['level_open'] = array('0' => '否', '1' => '是');
        $this->opt['team_post_open'] = array('0' => '否', '1' => '是');
        $this->opt['manager_open'] = array('0' => '否', '1' => '是');
        $teamAuditConfig = model('Xdata')->get('team_Admin:teamAuditConfig');
        // dump($teamAuditConfig);exit;
        $this->displayConfig($teamAuditConfig);
    }

    public function doTeamAuditConfig()
    {
        $list = $_POST['systemdata_list'];
        $key = $_POST['systemdata_key'];
        $key = $list.':'.$key;
        $value['apply_team_open'] = intval($_POST['apply_team_open']);
        $value['follower_open'] = intval($_POST['follower_open']);
        $value['follower'] = intval($_POST['follower']);
        $value['level_open'] = intval($_POST['level_open']);
        $value['level'] = intval($_POST['level']);
        $value['team_post_open'] = intval($_POST['team_post_open']);
        $value['team_post'] = intval($_POST['team_post']);
        $value['manager_open'] = intval($_POST['manager_open']);
        $res = model('Xdata')->put($key, $value);
        if ($res) {
            $this->assign('jumpUrl', U('team/Admin/teamAuditConfig', array('tabHash' => 'teamAuditConfig')));
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 队伍审核.
     */
    public function teamAudit()
    {
        $_REQUEST['tabHash'] = 'teamAudit';
        $this->_initTeamListAdminMenu();
        // 设置列表主键
        $this->_listpk = 'team_id';
        $this->pageButton[] = array('title' => '搜索', 'onclick' => "admin.fold('search_form')");
        $this->pageButton[] = array('title' => '通过', 'onclick' => "admin.doTeamAudit('', 1)");
        $this->pageButton[] = array('title' => '驳回', 'onclick' => "admin.doTeamAudit('', -1)");
        $this->searchKey = array('team_name');
        $this->pageKeyList = array('team_id', 'team_name', 'team_cate', 'info', 'uid', 'uinfo', 'live', 'ctime', 'DOACTION');
        if (!empty($_POST['team_name'])) {
            $maps['team_name'] = array('like', '%'.t($_POST['team_name']).'%');
            $map['team_id'] = array('in', getSubByKey(D('team')->where($maps)->field('team_id')->findAll(), 'team_id'));
        }
        $map['status'] = 0;
        // 数据的格式化与listKey保持一致
        $listData = D('team')->where($map)->order('ctime desc')->findPage(20);

        $teamcate = D('team_category')->findAll();
        $cids = array();
        foreach ($teamcate as $c) {
            $cids[$c['id']] = $c['name'];
        }
        foreach ($listData['data'] as $k => $v) {
            $listData['data'][$k]['team_name'] = '<a target="_blank" href="'.U('team/Index/detail', array('team_id' => $v['team_id'])).'">'.$v['team_name'].'</a>';
            $listData['data'][$k]['logo'] && $listData['data'][$k]['logo'] = '<img src="'.getImageUrlByAttachId($v['logo']).'" width="50" height="50">';
            $create_uid = model('User')->getUserInfoByUids($v['uid']);
            $listData['data'][$k]['uid'] = $create_uid[$v['uid']]['space_link'];
            $listData['data'][$k]['uinfo'] = $create_uid[$v['uid']]['group_icon'];
            $listData['data'][$k]['live'] = $create_uid[$v['uid']]['credit_info']['level']['name'];
            $listData['data'][$k]['ctime'] = friendlyDate($v['ctime']);
            $listData['data'][$k]['team_cate'] = $cids[$v['cid']];
            $listData['data'][$k]['DOACTION'] = '<a href="javascript:void(0)" onclick="admin.doTeamAudit('.$v['team_id'].',1);">通过</a>&nbsp;-&nbsp;<a href="javascript:void(0)" onclick="admin.doTeamAudit('.$v['team_id'].',-1);">驳回</a>';
        }
        $this->displayList($listData);
    }

    public function doTeamAudit()
    {
        if (empty($_POST['team_id'])) {
            $return['status'] = 0;
            $return['data'] = '';
            echo json_encode($return);
            exit();
        }
        !is_array($_POST['team_id']) && $_POST['team_id'] = array($_POST['team_id']);
        $map['team_id'] = array('in', $_POST['team_id']);
        $data['status'] = intval($_POST['value']);

        /* 如果小于1，是-1 则直接删除数据 */
        if (intval($_POST['value']) < 1) {
            $res = D('team')->where($map)->delete();

        /* # 否则通过 */
        } else {
            $res = D('team')->where($map)->save($data);
        }

        if ($res) {
            $return['status'] = 1;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');
        } else {
            $return['status'] = 0;
            $return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');
        }
        echo json_encode($return);
        exit();
    }

    /**
     * 队伍后台管理菜单.
     */
    private function _initTeamListAdminMenu()
    {
        $this->pageTab[] = array('title' => '队伍列表', 'tabHash' => 'index', 'url' => U('team/Admin/index'));
        $this->pageTab[] = array('title' => '添加队伍', 'tabHash' => 'addTeam', 'url' => U('team/Admin/addTeam'));
        $this->pageTab[] = array('title' => '队伍分类', 'tabHash' => 'teamCate', 'url' => U('team/Admin/teamCate'));
        $this->pageTab[] = array('title' => '帖子列表', 'tabHash' => 'postList', 'url' => U('team/Admin/postList'));
        $this->pageTab[] = array('title' => '帖子回收站', 'tabHash' => 'postRecycle', 'url' => U('team/Admin/postRecycle'));
        $this->pageTab[] = array('title' => '申请队长配置', 'tabHash' => 'teamAdminAuditConfig', 'url' => U('team/Admin/teamAdminAuditConfig'));
        $this->pageTab[] = array('title' => '队长审核', 'tabHash' => 'teamAdminAudit', 'url' => U('team/Admin/teamAdminAudit'));
        $this->pageTab[] = array('title' => '申请队伍配置', 'tabHash' => 'teamAuditConfig', 'url' => U('team/Admin/teamAuditConfig'));
        $this->pageTab[] = array('title' => '队伍审核', 'tabHash' => 'teamAudit', 'url' => U('team/Admin/teamAudit'));
        $this->pageTab[] = array('title' => '首页帖子', 'tabHash' => 'indexPost', 'url' => U('team/Admin/indexPost'));
    }
}
